# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
#   Mayor.create(:name => 'Daley', :city => cities.first)
sample_data = YAML::load(File.open("#{Rails.root}/db/sampledata.yml"))
sample_data["roles"].each do |item|
  Role.create(:name => item)
end

puts "Qa not created" unless Qa.create(sample_data["qas"])
puts "Receipt not created" unless Receipt.create(sample_data["receipts"])
puts "Novation not created" unless Novation.create(sample_data["novations"])
puts "Banner not created" unless Banner.create(sample_data["banners"])
puts "Desire not created" unless Desire.create(sample_data["desires"])
puts "Pagecont not created" unless Pagecont.create(sample_data["pageconts"])
puts "Inserting users"
sample_data["users"].each do |user_data|
  user = User.new(user_data)
  user.active = user_data["active"]
  user.approved = user_data["approved"]
  user.save
  puts user.errors
end if sample_data["users"]
puts "Inserting towns"
sample_data["towns"].each do |town_data|
  town = Town.new(town_data.reject do |key,value| key =~ /shops/ end)
  town_data["shops"].each do |shop_data|
    shop = Shop.new(shop_data.reject do |key, value| key =~ /staffs|categors/ end)
    shop_data["staffs"].each do |staff_data|
      staff = Staff.new(staff_data)
      shop.staffs << staff
    end if shop_data["staffs"]
    shop_data["categors"].each do |categor_data|
      categor = Categor.new(categor_data.reject do |key, value| key =~ /products/ end)
      categor_data["products"].each do |product_data|
        product = Product.new(product_data)
        categor.products << product
      end if categor_data["products"]
      shop.categors << categor
    end if shop_data["categors"]
    town.shops << shop
  end if town_data["shops"]
  town.save
  puts town.errors
end
